package com.cwvs.cat.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import net.tsz.afinal.FinalBitmap;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cwvs.cat.MsgInviteActivity;
import com.cwvs.cat.R;
import com.cwvs.cat.bean.MsgInvite;
import com.cwvs.cat.net.Port;

public class MsgInviteAdapter extends BaseAdapter {

	private Activity mContext;
	private List<MsgInvite> list_contact;
	private LayoutInflater inflater;
	private FinalBitmap fb;
	public MsgInviteAdapter() {
		// TODO Auto-generated constructor stub
	}

	public MsgInviteAdapter(Activity ac, List<MsgInvite> list) {
		// TODO Auto-generated constructor stub
		this.mContext=ac;
		this.inflater = LayoutInflater.from(ac);
		this.list_contact = list;
		fb = FinalBitmap.create(ac);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list_contact.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list_contact.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewholder;
		if(convertView==null){
			convertView = inflater.inflate(R.layout.item_list_msg_invite, null);
			viewholder = new ViewHolder();
			viewholder.txt_private_message_name=(TextView) convertView.findViewById(R.id.txt_private_message_name);
			viewholder.txt_private_message_content=(TextView) convertView.findViewById(R.id.txt_private_message_content);
			viewholder.time = (TextView) convertView.findViewById(R.id.time);
			viewholder.agree = (TextView) convertView.findViewById(R.id.agree);
			viewholder.refuse = (TextView) convertView.findViewById(R.id.refuse);
			viewholder.img_my_message_logo= (ImageView) convertView.findViewById(R.id.img_my_message_logo);
			convertView.setTag(viewholder);
			
		}else{
			viewholder = (ViewHolder) convertView.getTag();
		}
		String time=list_contact.get(position).read_time;  
	    Date date=null;  
	    SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	    try {
			date=formatter.parse(time);
			SimpleDateFormat formatter2 =new SimpleDateFormat("MM-dd HH:mm:ss");  
		    time=formatter2.format(date);  
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		viewholder.time.setText(time);
		viewholder.txt_private_message_name.setText(list_contact.get(position).title);
		viewholder.txt_private_message_content.setText(list_contact.get(position).content);
		if (!list_contact.get(position).avatar.equals("null")&&!list_contact.get(position).avatar.equals("")) {
			if (list_contact.get(position).avatar.contains("http")) {
				fb.display(viewholder.img_my_message_logo,list_contact.get(position).avatar);
			}else {
				fb.display(viewholder.img_my_message_logo,Port.getImg+list_contact.get(position).avatar);
			}
		}
		viewholder.agree.setOnClickListener(new onAgreeListener(list_contact.get(position).msg_id,Integer.parseInt(list_contact.get(position).type)));
		viewholder.refuse.setOnClickListener(new onRefuseListener(list_contact.get(position).msg_id,Integer.parseInt(list_contact.get(position).type)));
		return convertView;
	}
	
	public static class onAgreeListener implements View.OnClickListener{
		String msg_id;
		int type;
		public onAgreeListener(String msg_id,int type){
			this.msg_id = msg_id;
			this.type = type;
		}
		
		@Override
		public void onClick(View arg0) {
			MsgInviteActivity.agreeOrNot(msg_id, "1",type);
		}
		
	}
	public static class onRefuseListener implements View.OnClickListener{
		String msg_id;
		int type;
		public onRefuseListener(String msg_id,int type){
			this.msg_id = msg_id;
			this.type = type;
		}
		
		@Override
		public void onClick(View arg0) {
			MsgInviteActivity.agreeOrNot(msg_id, "0",type);
		}
		
	}
	class ViewHolder {
		TextView txt_private_message_name,txt_private_message_content,time,agree,refuse;
		ImageView img_my_message_logo;
	}

}
